package SwingComponents;

import Connection.IPServer;
import Controllers.SelectPhaseController;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SelectPhasePanel extends JPanel {

    private TransparentPanel begginingPanel;
    private TransparentPanel centerPanel;
    private TransparentPanel downPanel;
    private ArrayList<IDButton> jButtonList;
    private ArrayList<String> loadImagePhaseList;
    private SelectPhaseController selectPhaseController;

    public SelectPhasePanel(SelectPhaseController selectPhaseController, ArrayList<String> loadImagePhaseList) {
        this.setLayout(new BorderLayout());
        this.setBackground(Color.BLACK);
        this.setVisible(true);
        this.setSize(new Dimension(501, 701));
        this.setMaximumSize(new Dimension(501, 701));
        this.setMinimumSize(new Dimension(501, 701));
        this.setPreferredSize(new Dimension(501, 701));
        this.loadImagePhaseList = loadImagePhaseList;
        this.selectPhaseController = selectPhaseController;
        jButtonList = new ArrayList<>();
        initComponents();
    }

    public void initComponents() {
        try {
            JLabel selectPhaseLabel = new JLabel("Select Phase");
            selectPhaseLabel.setFont(new Font("Dialog", Font.PLAIN, 24));
            selectPhaseLabel.setForeground(Color.WHITE);

            begginingPanel = new TransparentPanel();
            begginingPanel.setSize(new Dimension(501, 100));
            begginingPanel.setMaximumSize(new Dimension(501, 100));
            begginingPanel.setMinimumSize(new Dimension(501, 100));
            begginingPanel.setPreferredSize(new Dimension(501, 100));
            begginingPanel.add(selectPhaseLabel);

            centerPanel = new TransparentPanel();
            centerPanel.setLayout(new GridLayout(3, 3));
            centerPanel.setSize(new Dimension(501, 543));
            centerPanel.setMinimumSize(new Dimension(501, 543));
            centerPanel.setMaximumSize(new Dimension(501, 543));
            centerPanel.setPreferredSize(new Dimension(501, 543));

            
            IPServer iPServer = new IPServer(selectPhaseController.getPrincipalController().getApplication());
            for (Integer i = 0; i < loadImagePhaseList.size(); i++) {
                IDButton auxButton = new IDButton(i, 30,65, "Phase");
                auxButton.setSize(new Dimension(167, 181));
                auxButton.setMinimumSize(new Dimension(167, 181));
                auxButton.setMaximumSize(new Dimension(167, 181));
                auxButton.setPreferredSize(new Dimension(167, 181));
                jButtonList.add(auxButton);
                jButtonList.get(i).setIcon(new ImageIcon(new URL(iPServer.getAdress() + loadImagePhaseList.get(i))));
                centerPanel.add(auxButton);
                jButtonList.get(i).addActionListener(selectPhaseController);
            }
            if (jButtonList.size() % 9 != 0) {
                while (jButtonList.size() % 9 != 0) {
                    IDButton auxButton = new IDButton(0, 0, 0, "");
                    auxButton.setSize(new Dimension(167, 181));
                    auxButton.setMinimumSize(new Dimension(167, 181));
                    auxButton.setMaximumSize(new Dimension(167, 181));
                    auxButton.setPreferredSize(new Dimension(167, 181));
                    auxButton.setBackground(Color.BLACK);
                    auxButton.setFocusPainted(false);
                    auxButton.setBorderPainted(false);
                    auxButton.setContentAreaFilled(false);
                    jButtonList.add(auxButton);
                    centerPanel.add(auxButton);
                }
            }

            JButton backButton = new JButton("Back");
            backButton.addActionListener(selectPhaseController);

            downPanel = new TransparentPanel();
            downPanel.setSize(new Dimension(501, 35));
            downPanel.setMaximumSize(new Dimension(501, 35));
            downPanel.setSize(new Dimension(501, 35));
            downPanel.setSize(new Dimension(501, 35));
            downPanel.add(backButton);

            this.add(begginingPanel, BorderLayout.PAGE_START);
            this.add(centerPanel, BorderLayout.CENTER);
            this.add(downPanel, BorderLayout.PAGE_END);
        } catch (Exception e) {
            try {
                File file = new File("C:/SelectPhasePanel_initComponents.txt");
                file.createNewFile();
            } catch (IOException ex1) {
            }
        }
    }

    public ArrayList<String> getLoadImagePhaseList() {
        return loadImagePhaseList;
    }
}
